package UI;

import Entity.ExamInfo;
import Entity.QuestionInfo;
import Entity.User;
import Service.ContentContext;
import Service.idOrPwdException;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class ChangeUI {

    private Login loginFrame;
    private Menu menuFrame;
    private Exam examFrame;
    private Welcome welcome;

    public void setWelcome(Welcome welcome) {
        this.welcome = welcome;
    }

    public void setExamFrame(Exam examFrame) {
        this.examFrame = examFrame;
    }

    public void setContentContext(ContentContext contentContext) {
        this.contentContext = contentContext;
    }

    private ContentContext contentContext;
    /*
        步骤二:创建一个登录的方法
            登录:用户和密码 -> LoginFrame
                1.login() -> 传参
                2.直接LoginFrame作为类的成员变量使用
                    可以通过getUserid() ...
       步骤三:页面的跳转 -> 显示主菜单页面
            MenuFrame.setVisible(true)
            LoginFrame.setVisiable(false)
       步骤四:附加功能:调用ContentContext中的login()
              将真正的用户和密码/数据传输进来进行判断
     */
    private int id;
    private String pwd;

    public int getId() {
        return id;
    }

    public String getPwd() {
        return pwd;
    }
    private User s;
    public void login() {
        //假设数据 id = 123  pwd = 456
        /*
            真实数据从 ContentContext中获取
         */
        try {
            //获取id
            id = loginFrame.getUserId();
            //获取密码
            pwd = loginFrame.getUserPwd();
            s= contentContext.correct(id);
            menuFrame.updateView(s.getName());
            menuFrame.setVisible(true);
            loginFrame.setVisible(false);
        } catch (idOrPwdException e) {
            loginFrame.showError(e.getMessage());
        } catch (NumberFormatException e){
            loginFrame.showError("账号只能为整数");
        }

        //界面的切换 -> 让主界面显示出来



//        if(id == 123 && pwd.equals("456")){
//            //修改menuFrame的用户信息
//            menuFrame.updateView("张三");
//            //界面的切换 -> 让主界面显示出来
//            menuFrame.setVisible(true);
//            loginFrame.setVisible(false);
//        }
    }

    //________________________以下都是依赖注入部分


    public void setLogin(Login loginFrame) {
        this.loginFrame = loginFrame;
    }

    public void setMenu(Menu menuFrame) {
        this.menuFrame = menuFrame;
    }

    /*
        程序开始就显示的界面
     */
    public void showMe() {
        welcome.setVisible(true);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                welcome.setVisible(false);
                loginFrame.setVisible(true);
                timer.cancel();
            }
        }, 2000);
    }

    public void showRules() {
        JOptionPane.showMessageDialog(null, contentContext.getRules(),"考场规则",JOptionPane.INFORMATION_MESSAGE);
    }

    public void exit() {
        int i = JOptionPane.showConfirmDialog(null, "是否退出");
        if (i==JOptionPane.YES_OPTION){
            System.exit(0);
        }
    }

    private QuestionInfo currentQuestionInfo;

    private ExamInfo examInfo;

    public void enterTest() {
        try {
            // 访问业务层开始考试
            examInfo = contentContext.start();
            // 取得第一道题, 用于显示考题
            currentQuestionInfo = contentContext.getQuestion(0);
            // 更新考试界面
            examFrame.updateView(examInfo, currentQuestionInfo);
            examFrame.updateUser(s.getName());
            // 关闭菜单,
            menuFrame.setVisible(false);
            // 打开考试界面
            examFrame.setVisible(true);
            startTimer();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(menuFrame, e.getMessage());
        }
    }
    private Timer timer=new Timer();
    private void startTimer() {
        final long end = System.currentTimeMillis() + 1000 * 60 * 2;
        timer.schedule(new TimerTask() {
            public void run() {
                long now = System.currentTimeMillis();
                long show = end - now;
                long h = show / 1000 / 60 / 60;
                long m = show / 1000 / 60 % 60;
                long s = show / 1000 % 60;
                examFrame.showTime(h, m, s);
            }
        }, 0, 1000);
        timer.schedule(new TimerTask() {
            public void run() {
                gameOver();
            }
        }, new Date(end));
    }
    public void next() {
        try {
            // 取得当前的题号
            int index = currentQuestionInfo.getQuestionIndex();
            // 保存当前考题的用户答案到业务层
            List<Integer> userAnswers = examFrame.getUserAnswers();
            contentContext.saveUserAnswers(index, userAnswers);
            // 取得下一题目
            QuestionInfo questionInfo = contentContext.getQuestion(index + 1);
            currentQuestionInfo = questionInfo;
            // 更新考试界面, 显示下一题目
            examFrame.updateView(examInfo, questionInfo);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(examFrame, e.getMessage());
        }
    }

    public void prev() {
        try {
            // 取得当前的题号
            int index = currentQuestionInfo.getQuestionIndex();
            // 保存当前考题的用户答案到业务层
            List<Integer> userAnswers = examFrame.getUserAnswers();
            contentContext.saveUserAnswers(index, userAnswers);
            // 取得下一题目
            QuestionInfo questionInfo = contentContext.getQuestion(index - 1);
            currentQuestionInfo = questionInfo;
            // 更新考试界面, 显示上一题目
            examFrame.updateView(examInfo, questionInfo);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(examFrame, e.getMessage());
        }
    }

    public void send() {
        int val = JOptionPane.showConfirmDialog(examFrame, "交卷?");
        if (val == JOptionPane.YES_OPTION) {
            gameOver();
        }
    }

    private void gameOver() {
        try {
            // 获取最后的用户答案
            List<Integer> ans = examFrame.getUserAnswers();
            int index = currentQuestionInfo.getQuestionIndex();
            // 保存用户答案
            contentContext.saveUserAnswers(index, ans);
            // 考试结束, 得到分数
            int score = contentContext.examOver();
            // 显示分数
            JOptionPane.showMessageDialog(examFrame, "分数:" + score);
            // 关闭考试界面
            examFrame.setVisible(false);
            // 返回菜单界面
            menuFrame.setVisible(true);
            timer.cancel();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(examFrame, e.getMessage());
        }
    }
}
